package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysCloseDataAddBo;
import com.xjy.system.domain.bo.SysCloseDataQueryBo;
import com.xjy.system.domain.bo.SysCloseDataEditBo;
import com.xjy.system.domain.SysCloseData;
import com.xjy.system.mapper.SysCloseDataMapper;
import com.xjy.system.domain.vo.SysCloseDataVo;
import com.xjy.system.service.ISysCloseDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 结账数据信息Service业务层处理
 *
 * @author xjy
 * @date 2022-03-10
 */
@Service
public class SysCloseDataServiceImpl extends ServicePlusImpl<SysCloseDataMapper, SysCloseData> implements ISysCloseDataService {

	@Override
	public SysCloseDataVo queryById(Long id) {
		return getVoById(id, SysCloseDataVo.class);
	}

	@Override
	public TableDataInfo<SysCloseDataVo> queryPageList(SysCloseDataQueryBo bo) {
		PagePlus<SysCloseData, SysCloseDataVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysCloseDataVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SysCloseDataVo> queryList(SysCloseDataQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysCloseDataVo.class);
	}

	private LambdaQueryWrapper<SysCloseData> buildQueryWrapper(SysCloseDataQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysCloseData> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getProjId() != null, SysCloseData::getProjId, bo.getProjId());
		lqw.eq(bo.getContractId() != null, SysCloseData::getContractId, bo.getContractId());
		lqw.eq(bo.getResultProjId() != null, SysCloseData::getResultProjId, bo.getResultProjId());
		lqw.eq(bo.getSurplusAmt() != null, SysCloseData::getSurplusAmt, bo.getSurplusAmt());
		lqw.eq(bo.getCloseStartTime() != null, SysCloseData::getCloseStartTime, bo.getCloseStartTime());
		lqw.eq(bo.getCloseEndTime() != null, SysCloseData::getCloseEndTime, bo.getCloseEndTime());
		lqw.eq(bo.getCloseTime() != null, SysCloseData::getCloseTime, bo.getCloseTime());
		lqw.eq(StrUtil.isNotBlank(bo.getOperator()), SysCloseData::getOperator, bo.getOperator());
		lqw.eq(bo.getOperatorId() != null, SysCloseData::getOperatorId, bo.getOperatorId());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysCloseDataAddBo bo) {
		SysCloseData add = BeanUtil.toBean(bo, SysCloseData.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysCloseDataEditBo bo) {
		SysCloseData update = BeanUtil.toBean(bo, SysCloseData.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysCloseData entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
